﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;

namespace WinFormsApp1.TongShiWei
{
    public partial class ShowPRQSData : UIForm
    {
        public ShowPRQSData()
        {
            InitializeComponent();
            ShowPRQSData_();
        }
        int AddNum = 0;
        List<SHowPRQSData> pRQSData = new List<SHowPRQSData>();
        public async void ShowPRQSData_()
        {
            GetPRQSData PRQSData = new GetPRQSData();
            PRQSData.PRQSName = GetPRQSName.Text;
            //http://localhost:5159/api/TSW/ShowPRQSData
            var res = await HttpClientHelper.RequestUrl<GetPRQSData, ApiResult<List<SHowPRQSData>>>(URLCommon.Url2, "/TSW/ShowPRQSData", HttpClientHelper.MethodType.POST, PRQSData);
            if (res.Code > 0)
            {
                pRQSData = res.Data;
                foreach (var item in pRQSData)
                {
                    PRQSListData.Rows.Add(item.PRQSId, item.PRQSName, item.PRQSindex, item.Require, (item.PRQSstate == true ? "禁用" : "启用"), item.CreateName, item.CreateTime.ToString("yyyy-MM-dd"), item.FinallyName, item.FinallyTime.ToString("yyyy-MM-dd"));
                }
                if (AddNum == 0)
                {
                    //禁用
                    DataGridViewButtonColumn ForbidButton = new DataGridViewButtonColumn();
                    ForbidButton.Text = "禁用";
                    ForbidButton.UseColumnTextForButtonValue = true;
                    //修改
                    DataGridViewButtonColumn UpdateButton = new DataGridViewButtonColumn();
                    UpdateButton.HeaderText = "操作";
                    UpdateButton.Text = "修改";
                    UpdateButton.UseColumnTextForButtonValue = true;
                    //启动 
                    DataGridViewButtonColumn DeleteButton = new DataGridViewButtonColumn();
                    DeleteButton.Text = "删除";
                    DeleteButton.UseColumnTextForButtonValue = true;
                    PRQSListData.Columns.AddRange(ForbidButton, UpdateButton, DeleteButton);
                    AddNum += 1;
                    PRQSListData.CellClick += new DataGridViewCellEventHandler(PRQSListData_CellContentClick);
                }
            }
        }
        /// <summary>
        /// 修改次数
        /// </summary>
        int UpNum = 0;
        public async void PRQSListData_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!(e.ColumnIndex >= 9))
            {
                return;
            }
            switch (e.ColumnIndex)
            {
                case (9 or 11):
                    var data = PRQSListData.Rows[e.RowIndex].Cells[0].Value.ToString();
                    UpPRQSData upPRQSData = new UpPRQSData()
                    {
                        Pid = e.ColumnIndex,
                        PRQSId = Convert.ToInt32(data)
                    };
                    //http://localhost:5159/api/TSW/DelDrisableData
                    var res = await HttpClientHelper.RequestUrl<UpPRQSData, ApiResult<List<SHowPRQSData>>>(URLCommon.Url1, "/TSW/DelDrisableData", HttpClientHelper.MethodType.POST, upPRQSData);
                    bool PRQSstate = PRQSListData.Rows[e.RowIndex].Cells[4].Value.ToString() == "禁用" ? false : true;
                    if (PRQSstate == false)
                    {
                        if (res.Code > 0 && e.ColumnIndex == 9)
                        {
                            if (UpNum > 0)
                            {
                                return;
                            }
                            UpNum = 1;
                            MessageBox.Show("启动成功");
                        }
                        else if (res.Code == 0 && e.ColumnIndex == 9)
                        {
                            MessageBox.Show("启动失败");
                        }
                        else if (res.Code > 0 && e.ColumnIndex == 11)
                        {
                            if (UpNum > 0)
                            {
                                return;
                            }
                            UpNum = 1;
                            MessageBox.Show("删除成功");
                        }
                        else
                        {
                            MessageBox.Show("删除失败");
                        }
                    }
                    else
                    {
                        if (res.Code > 0 && e.ColumnIndex == 9)
                        {
                            if (UpNum > 0)
                            {
                                return;
                            }
                            UpNum = 1;
                            MessageBox.Show("禁用成功");
                        }
                        else if (res.Code == 0 && e.ColumnIndex == 9)
                        {
                            MessageBox.Show("禁用失败");
                        }
                        else if (res.Code > 0 && e.ColumnIndex == 11)
                        {
                            if (UpNum > 0)
                            {
                                return;
                            }
                            UpNum = 1;
                            MessageBox.Show("删除成功");
                        }
                        else
                        {
                            MessageBox.Show("删除失败");
                        }
                    }
                    break;
                case 10:

                    break;
            }
            if (UpNum > 0)
            {
                for (int i = PRQSListData.Rows.Count - 2; i >=0; i--)
                {
                    PRQSListData.Rows.RemoveAt(i);
                }
                UpNum = 0;
                ShowPRQSData_();
            }
        }
        private void uiLabel1_Click(object sender, EventArgs e)
        {

        }

        private void uiButton5_Click(object sender, EventArgs e)
        {
            AddPRQSData addPRQSData = new AddPRQSData();
            addPRQSData.Show();
        }
        public class SHowPRQSData
        {
            /// <summary>
            /// 检疫项目Id
            /// </summary>
            public int PRQSId { get; set; }
            /// <summary>
            /// 检疫项目名称
            /// </summary>
            public string PRQSName { get; set; } = string.Empty;
            /// <summary>
            /// 检疫指标
            /// </summary>
            public string PRQSindex { get; set; } = string.Empty;
            /// <summary>
            /// 标准要求
            /// </summary>
            public string Require { get; set; } = string.Empty;
            /// <summary>
            /// 启用状态
            /// </summary>
            public bool PRQSstate { get; set; } = false;
            /// <summary>
            /// 创建人
            /// </summary>
            public string CreateName { get; set; } = string.Empty;
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime CreateTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 最后修改人
            /// </summary>
            public string FinallyName { get; set; } = string.Empty;
            /// <summary>
            /// 最后修改时间
            /// </summary>
            public DateTime FinallyTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 是否禁用
            /// </summary>
            public bool IsPRQS { get; set; } = false;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool ISDeletePRQS { get; set; } = false;
        }
        public class GetPRQSData
        {
            public string PRQSName { get; set; }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton2_Click(object sender, EventArgs e)
        {
            var PRQSNum = PRQSListData.Rows.Count - 2;
            for (int i = PRQSNum; i >=0; i--)
            { 
                PRQSListData.Rows.RemoveAt(i);
            }
            ShowPRQSData_();
        }

        /// <summary>
        /// 修改或删除检测项目表
        /// </summary>
        public class UpPRQSData
        {
            public int Pid { get; set; }
            public int PRQSId {  get; set; }
        }
    }
}
